/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.Component;

import com.bourre.events.EventBroadcaster;
import com.bourre.events.EventType;
import com.bourre.transitions.ITween;
import com.bourre.transitions.TweenEvent;
import com.bourre.transitions.TweenMS;

import fever.exception.IllegalAccessException;

import fvaswing.effects.FvEffectEvent;
import fvaswing.effects.FvEffectInstance;

/**
 * Abstract class to manage tween effects on <strong>ASwing Components</strong>
 * 
 * <p>Take a look at <strong>fvaswing.effects</strong> package to see concrete 
 * effect implementations.
 * 
 * <p>Use the Pixlib Tween engine.<br />
 * More informations about <strong>Pixlib</strong> <a href="http://www.osflash.org/pixlib">here</a>.
 *  
 * @author Romain Ecarnot
 */
class fvaswing.effects.FvTweenEffect extends EventBroadcaster
{
	//-------------------------------------------------------------------------
	// Events definition
	//-------------------------------------------------------------------------
	
	/** Broadcasted when effect stop playing. */
	public static var onEffectEndEVENT : EventType = FvEffectEvent.onEffectEndEVENT;
	
	/** Broadcasted when effect is playing. */
	public static var onEffectProgressEVENT : EventType = FvEffectEvent.onEffectProgressEVENT;
	
	/** Broadcasted when effect start playing. */
	public static var onEffectStartEVENT : EventType = FvEffectEvent.onEffectStartEVENT;
	
	
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _instance : FvEffectInstance;
	
	private var _tween : ITween;
	private var _duration : Number;
	private var _easing : Function;
	private var _event : FvEffectEvent;
	private var _isPlaying : Boolean;	
	
	private var _firstPlay : Boolean;	private var _isRepeated : Boolean;
	
	private var _counter : Number;
	private var _repeatRef : Number;
	private var _waitInterval : Number;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/**
	 * Defines the duration of the effect in milliseconds.
	 * 
	 * <p>The default value is 500 milliseconds.
	 */
	public function set duration( value : Number  ) : Void { _duration = value; }
	
	/**
	 * Returns the duration of the effect in milliseconds.
	 */
	public function get duration() : Number { return _duration; }
	
	/**
	 * Defines the easing function for the animation.
	 */
	public function set easing( value : Function  ) : Void { _easing = value; }
	
	/**
	 * Returns current easing function used by effect.
	 */
	public function get easing() : Function { return _easing; }
	
	/**
	 * A read-only flag which is true if any instances of the effect are currently 
	 * playing, and false if none are.
	 */
	public function get isPlaying() : Boolean { return _isPlaying; }
	
	/**
	 * Number of effect repetitions.
	 * Possible values are any integer greater than or equal to 0.
	 * 
	 * <p>A value of 1 means to play the effect once. 
	 * 
	 * <p>A value of 0 means to play the effect indefinitely 
	 * until stopped by a call to the end() method.
	 */
	public var repeat : Number = 1;
	
	/**
	 * Amount of time, in milliseconds, to wait before repeating the effect.
	 * 
	 * <p>Possible values are any integer greater than or equal to 0.
	 */	public var repeatDelay : Number = 0;
	
	/**
	 * Amount of time, in milliseconds, to wait before starting the effect.
	 * 
	 * <p>Possible values are any int greater than or equal to 0.
	 * 
	 * <p>If the effect is repeated by using the repeat property, 
	 * the startDelay is only applied to the first time the effect is played.
	 */	public var startDelay : Number = 0;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Starts effect.
	 * 
	 * <p>{@link #onEffectStartEVENT} event is dispatched
	 */
	public function play() : Void
	{
		if( ! _isPlaying )
		{
			_isRepeated = false;
			_counter = 0;
			
			if( startDelay > 0 && _firstPlay ) 
			{
				_firstPlay = false;
				_waitInterval = setInterval( this, '_prepareEffect', startDelay );
			}
			else _prepareEffect();
		}
	}
	
	/**
	 * Ends effects.
	 * 
	 * <p>Must be override by custom subclass
	 * 
	 * <p>{@link #onEffectEndEVENT} event is dispatched
	 * 
	 * @throws Error if subclass not override this method
	 */
	public function end() : Void
	{
		throw new IllegalAccessException( 'must be override by Effect subclass' );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 * 
	 * @param t AsVing component target
	 */
	private function FvTweenEffect( t : Component )
	{
		super( this );
		
		_instance = new FvEffectInstance( t );
		
		_duration = 500; 
		_easing = TweenMS.noEasing;
		_event = new FvEffectEvent( null, this );
		_isPlaying = false;
		_isRepeated = false;
		
		repeat = 1;
		repeatDelay = 0;		startDelay = 0;
		
		_firstPlay = true;
	}
	
	/**
	 * Retreives correct starting and ending values.<br />
	 * Reset the repetitions counter
	 */
	private function _prepareEffect() : Void
	{
		clearInterval( _waitInterval );
		_buildTween();
		
		_repeatRef = repeat;
		_counter++;
		
		_play();
	}
	
	private function _play() : Void
	{
		_isPlaying = true;
		_tween.start();
		
		_event.setType( FvTweenEffect.onEffectStartEVENT );
		broadcastEvent( _event );
	}
	
	/**
	 * Builds tweens instance
	 * 
	 * <p>Must be override by effect subclass.
	 * 
	 * @throws Error if subclass not override this method
	 */
	private function _buildTween() : Void
	{
		throw new IllegalAccessException( 'must be override by Effect subclass' );
	}
	
	/**
	 * Tween is finished.
	 * 
	 * <p>Looks if repetition is enabled and defined.
	 * If {@code true}, start loop process, either call 
	 * _endEffet() method.
	 */
	private function onMotionFinished( event : TweenEvent ) : Void
	{
		if( !_checkForRepeatEffect() ) _endEffect();
		else _repeatEffect();
	}
	
	/**
	 * Tweened properties has changed.
	 * 
	 * <p>Broadcasts {@link #onEffectProgressEVENT} event
	 */
	private function onMotionChanged( event : TweenEvent ) : Void
	{
		_event.setType( FvTweenEffect.onEffectProgressEVENT );
		_instance.target.revalidateIfNecessary();
		
		broadcastEvent( _event );
	}
	
	/**
	 * Indicates if repetition mode is enabled.
	 */
	private function _checkForRepeatEffect() : Boolean
	{
		return ( _repeatRef == 0 || ( _counter < _repeatRef ) );	
	}
	
	/**
	 * Repeats tweening
	 */
	private function _repeatEffect() : Void
	{
		_isRepeated = true;
		if( repeatDelay > 0 ) _waitInterval = setInterval( this, "_prepareEffect", repeatDelay );
		else _prepareEffect();
	}
	
	/**
	 * Ends tweening process.
	 * 
	 * <p>Broadcasts {@link #onEffectEndEVENT} event
	 */
	private function _endEffect() : Void
	{
		_isPlaying = false;
		_isRepeated = false;
		
		_instance.target.revalidateIfNecessary();
		
		_event.setType( FvTweenEffect.onEffectEndEVENT );
		broadcastEvent( _event );
	}
}